package com.hjm.project.bankExpenditure.gbTradeInfoTemp.service.impl;

import java.io.File;
import java.util.List;
import java.util.Map;

import cn.hutool.core.text.csv.CsvRow;
import com.google.common.collect.Lists;
import com.hjm.common.utils.CsvUtils;
import com.hjm.project.agentInfo.profitSbOrder.utils.PublicTypeUtils;
import com.hjm.project.bankExpenditure.gbTradeInfoTemp.domain.*;
import com.hjm.project.formalPos.formalPos.domain.FormalPos;
import com.hjm.project.formalPos.formalPos.mapper.FormalPosMapper;
import com.hjm.project.terminal.manufacturer.domain.Manufacturer;
import com.hjm.project.terminal.manufacturer.mapper.ManufacturerMapper;
import org.apache.commons.io.FileUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.hjm.project.bankExpenditure.gbTradeInfoTemp.mapper.GbTradeInfoTempMapper;
import com.hjm.project.bankExpenditure.gbTradeInfoTemp.service.IGbTradeInfoTempService;
import com.hjm.common.utils.text.Convert;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 临时交易Service业务层处理
 * 
 * @author maker-platform
 * @date 2024-03-11
 */
@Service
public class GbTradeInfoTempServiceImpl implements IGbTradeInfoTempService {
    @Resource
    private GbTradeInfoTempMapper gbTradeInfoTempMapper;
    @Resource
    private ManufacturerMapper manufacturerMapper;
    @Resource
    private FormalPosMapper formalPosMapper;

    /**
     * 查询临时交易
     * 
     * @param agentno 临时交易ID
     * @return 临时交易
     */
    @Override
    public GbTradeInfoTemp selectGbTradeInfoTempById(String agentno) {
        return gbTradeInfoTempMapper.selectGbTradeInfoTempById(agentno);
    }

    /**
     * 查询临时交易列表
     * 
     * @param gbTradeInfoTemp 临时交易
     * @return 临时交易
     */
    @Override
    public List<GbTradeInfoTemp> selectGbTradeInfoTempList(GbTradeInfoTemp gbTradeInfoTemp) {
        return gbTradeInfoTempMapper.selectGbTradeInfoTempList(gbTradeInfoTemp);
    }

    @Override
    public Map selectGbTradeInfoListCount(GbTradeInfoTemp gbTradeInfoTemp) {
        return gbTradeInfoTempMapper.selectGbTradeInfoListCount(gbTradeInfoTemp);
    }

    /**
     * 查询临时交易列表
     *
     * @param gbTradeInfoTemp 临时交易
     * @return 临时交易 一个结果
     */
    @Override
    public GbTradeInfoTemp selectGbTradeInfoTempOne(GbTradeInfoTemp gbTradeInfoTemp) {
        return gbTradeInfoTempMapper.selectGbTradeInfoTempOne(gbTradeInfoTemp);
    }

    /**
     * 新增临时交易
     * 
     * @param gbTradeInfoTemp 临时交易
     * @return 结果
     */
    @Override
    public int insertGbTradeInfoTemp(GbTradeInfoTemp gbTradeInfoTemp) {
        return gbTradeInfoTempMapper.insertGbTradeInfoTemp(gbTradeInfoTemp);
    }

    /**
     * 修改临时交易
     * 
     * @param gbTradeInfoTemp 临时交易
     * @return 结果
     */
    @Override
    public int updateGbTradeInfoTemp(GbTradeInfoTemp gbTradeInfoTemp) {
        return gbTradeInfoTempMapper.updateGbTradeInfoTemp(gbTradeInfoTemp);
    }

    /**
     * 删除临时交易对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteGbTradeInfoTempByIds(String ids) {
        return gbTradeInfoTempMapper.deleteGbTradeInfoTempByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除临时交易信息
     * 
     * @param agentno 临时交易ID
     * @return 结果
     */
    @Override
    public int deleteGbTradeInfoTempById(String agentno) {
        return gbTradeInfoTempMapper.deleteGbTradeInfoTempById(agentno);
    }

    @Async
    @Override
    public Integer importTemplate(MultipartFile file, Integer tradeType) {
        if (tradeType == 2) {
            try {
                File files = new File(file.getOriginalFilename());
                FileUtils.copyInputStreamToFile(file.getInputStream(), files);
                List<CsvRow> csvRows = CsvUtils.readDataFromCsv(files.getPath());
                for (int i = 0; i < csvRows.size() ; i++) {
                    if (i == 0) {
                        continue;
                    }
                    CsvRow csvRow = csvRows.get(i);
                    String localdate = csvRow.get(7);
                    String ldMerno = csvRow.get(1);
                    GbTradeInfoTemp gt = new GbTradeInfoTemp();
                    gt.setStatus(0);
                    gt.setAucType(tradeType.toString());
                    gt.setMerno(ldMerno);
                    gt.setMername(csvRow.get(2));
                    gt.setAmount(csvRow.get(9));
                    gt.setAgentno(csvRow.get(3));
                    gt.setPan(csvRow.get(8));
                    gt.setMsgtype(PublicTypeUtils.getMsgTypeToStr(csvRow.get(5)));
                    gt.setCardtype(PublicTypeUtils.getCardTypeToStr(csvRow.get(11)));
                    gt.setRrno(csvRow.get(0));
                    gt.setLocaldate(localdate);
                    gt.setLocaltime("");
                    FormalPos formalPos = formalPosMapper.selectFormalPosByLdMerno(ldMerno);
                    String sn = "";
                    String manuNum = "";
                    String manuName = "";
                    String posType = "";
                    if (formalPos != null) {
                        sn = formalPos.getMachineNum();
                        manuNum = formalPos.getManuNum();
                        posType = formalPos.getPosType().toString();
                        Manufacturer manufacturer = manufacturerMapper.selectManufacturerById(Long.parseLong(manuNum));
                        manuName = manufacturer.getManuName();

                    }
                    gt.setPosType(posType);
                    gt.setSn(sn);
                    gt.setManuNum(manuNum);
                    gt.setManuName(manuName);
                    gbTradeInfoTempMapper.deleteTrade(gt.getRrno());
                    gbTradeInfoTempMapper.insertGbTradeInfoTemp(gt);
                    gbTradeInfoTempMapper.deleteTradeError(localdate,tradeType.toString(),gt.getRrno());
                    gbTradeInfoTempMapper.updateTradeError(localdate,tradeType.toString(),gt.getRrno());
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //List<GbTradeInfoGbTemp> gbList = util.importExcel(file.getInputStream());

        return 0;
    }

    @Async
    @Override
    public Integer importTemplateHdb(List<GbTradeInfoHdbTemp> userList, Integer tradeType) throws Exception {
        for (GbTradeInfoHdbTemp data : userList) {
            if ("交易失败".equals(data.getTempStatusStr())) {
                continue;
            }
            GbTradeInfoTemp gt = new GbTradeInfoTemp();
            gt.setStatus(0);
            gt.setAucType(tradeType.toString());
            gt.setMerno(data.getMerno());
            gt.setMername(data.getMername());
            gt.setAmount(data.getAmount());
            gt.setAgentno(data.getAgentno());
            gt.setPan(data.getPan());
            gt.setMsgtype("H007");
            gt.setCardtype(PublicTypeUtils.getCardTypeToStr(data.getCardtype()));
            gt.setRrno(data.getRrno());
            gt.setLocaldate(data.getLocaldate().substring(0,10));
            gt.setLocaltime(data.getLocaldate().substring(11,19));
            FormalPos formalPos = formalPosMapper.selectFormalPosByLdMerno(gt.getMerno());
            String sn = "";
            String manuNum = "";
            String manuName = "";
            String posType = "";
            if (formalPos != null) {
                sn = formalPos.getMachineNum();
                manuNum = formalPos.getManuNum();
                posType = formalPos.getPosType().toString();
                Manufacturer manufacturer = manufacturerMapper.selectManufacturerById(Long.parseLong(manuNum));
                manuName = manufacturer.getManuName();

            }
            gt.setPosType(posType);
            gt.setSn(sn);
            gt.setManuNum(manuNum);
            gt.setManuName(manuName);
            gbTradeInfoTempMapper.deleteTrade(gt.getRrno());
            gbTradeInfoTempMapper.insertGbTradeInfoTemp(gt);
            gbTradeInfoTempMapper.deleteTradeError(data.getLocaldate().substring(0,10),tradeType.toString(),gt.getRrno());
            gbTradeInfoTempMapper.updateTradeError(data.getLocaldate().substring(0,10),tradeType.toString(),gt.getRrno());
        }

        //List<GbTradeInfoGbTemp> gbList = util.importExcel(file.getInputStream());

        return 0;
    }

    @Async
    @Override
    public Integer importTemplateZf(List<GbTradeInfoZfTemp> userList, Integer tradeType) throws Exception {
        List<List<GbTradeInfoZfTemp>> partition = Lists.partition(userList, 10);
        for (List<GbTradeInfoZfTemp> gbTradeInfoZfTemps : partition) {
            for (GbTradeInfoZfTemp data : gbTradeInfoZfTemps) {
                if ("交易失败".equals(data.getTempStatusStr())) {
                    continue;
                }
                GbTradeInfoTemp gt = new GbTradeInfoTemp();
                gt.setStatus(0);
                gt.setAucType(tradeType.toString());
                gt.setMerno(data.getMerno());
                gt.setMername(data.getMername());
                gt.setAmount(data.getAmount());
                gt.setAgentno(data.getAgentno());
                gt.setPan(data.getPan());
                gt.setMsgtype("H007");
                gt.setCardtype(PublicTypeUtils.getCardTypeToStr(data.getCardtype()));
                gt.setRrno(data.getRrno());
                gt.setLocaldate(data.getLocaldate().substring(0,10));
                gt.setLocaltime(data.getLocaldate().substring(11,19));
                FormalPos formalPos = formalPosMapper.selectFormalPosByLdMerno(gt.getMerno());
                String sn = "";
                String manuNum = "";
                String manuName = "";
                String posType = "";
                if (formalPos != null) {
                    sn = formalPos.getMachineNum();
                    manuNum = formalPos.getManuNum();
                    posType = formalPos.getPosType().toString();
                    Manufacturer manufacturer = manufacturerMapper.selectManufacturerById(Long.parseLong(manuNum));
                    manuName = manufacturer.getManuName();

                }
                gt.setPosType(posType);
                gt.setSn(sn);
                gt.setManuNum(manuNum);
                gt.setManuName(manuName);
                gbTradeInfoTempMapper.deleteTrade(gt.getRrno());
                gbTradeInfoTempMapper.insertGbTradeInfoTemp(gt);
                gbTradeInfoTempMapper.deleteTradeError(data.getLocaldate().substring(0,10),tradeType.toString(),gt.getRrno());
                gbTradeInfoTempMapper.updateTradeError(data.getLocaldate().substring(0,10),tradeType.toString(),gt.getRrno());
            }

        }

        //List<GbTradeInfoGbTemp> gbList = util.importExcel(file.getInputStream());

        return 0;
    }
    public Integer importTemplateLs(List<GbTradeInfoLsTemp> userList, Integer tradeType) throws Exception {
        List<List<GbTradeInfoLsTemp>> partition = Lists.partition(userList, 10);
        for (List<GbTradeInfoLsTemp> gbTradeInfoLsTemps : partition) {
            for (GbTradeInfoLsTemp data : gbTradeInfoLsTemps) {
                if ("失败".equals(data.getTempStatusStr())) {
                    continue;
                }
                GbTradeInfoTemp gt = new GbTradeInfoTemp();
                gt.setStatus(0);
                gt.setAucType(tradeType.toString());
                gt.setMerno(data.getMerno());
                gt.setMername(data.getMername());
                gt.setAmount(data.getAmount());
                gt.setAgentno(data.getAgentno());
                gt.setPan(data.getPan());
                gt.setMsgtype("H007");
                gt.setCardtype(PublicTypeUtils.getCardTypeToStr(data.getCardtype()));
                gt.setRrno(data.getRrno());
                gt.setLocaldate(data.getLocaldate().substring(0,10));
                gt.setLocaltime(data.getLocaldate().substring(11,19));
                FormalPos formalPos = formalPosMapper.selectFormalPosByLdMerno(gt.getMerno());
                String sn = "";
                String manuNum = "";
                String manuName = "";
                String posType = "";
                if (formalPos != null) {
                    sn = formalPos.getMachineNum();
                    manuNum = formalPos.getManuNum();
                    posType = formalPos.getPosType().toString();
                    Manufacturer manufacturer = manufacturerMapper.selectManufacturerById(Long.parseLong(manuNum));
                    manuName = manufacturer.getManuName();

                }
                gt.setPosType(posType);
                gt.setSn(sn);
                gt.setManuNum(manuNum);
                gt.setManuName(manuName);
                gbTradeInfoTempMapper.deleteTrade(gt.getRrno());
                gbTradeInfoTempMapper.insertGbTradeInfoTemp(gt);
                gbTradeInfoTempMapper.deleteTradeError(data.getLocaldate().substring(0,10),tradeType.toString(),gt.getRrno());
                gbTradeInfoTempMapper.updateTradeError(data.getLocaldate().substring(0,10),tradeType.toString(),gt.getRrno());
            }

        }

        //List<GbTradeInfoGbTemp> gbList = util.importExcel(file.getInputStream());

        return 0;
    }
    public Integer importTemplateQcf(List<GbTradeInfoQcfTemp> userList, Integer tradeType) throws Exception {
        List<List<GbTradeInfoQcfTemp>> partition = Lists.partition(userList, 10);
        for (List<GbTradeInfoQcfTemp> gbTradeInfoLsTemps : partition) {
            for (GbTradeInfoQcfTemp data : gbTradeInfoLsTemps) {
                if ("失败".equals(data.getTempStatusStr())) {
                    continue;
                }
                GbTradeInfoTemp gt = new GbTradeInfoTemp();
                gt.setStatus(0);
                gt.setAucType(tradeType.toString());
                gt.setMerno(data.getMerno());
                gt.setMername(data.getMername());
                gt.setAmount(data.getAmount());
                gt.setAgentno(data.getAgentno());
                gt.setPan(data.getPan());
                gt.setMsgtype("H007");
                gt.setCardtype(PublicTypeUtils.getCardTypeToStr(data.getCardtype()));
                gt.setRrno(data.getRrno());
                gt.setLocaldate(data.getLocaldate().substring(0,10));
                gt.setLocaltime(data.getLocaldate().substring(11,19));
                FormalPos formalPos = formalPosMapper.selectFormalPosByLdMerno(gt.getMerno());
                String sn = "";
                String manuNum = "";
                String manuName = "";
                String posType = "";
                if (formalPos != null) {
                    sn = formalPos.getMachineNum();
                    manuNum = formalPos.getManuNum();
                    posType = formalPos.getPosType().toString();
                    Manufacturer manufacturer = manufacturerMapper.selectManufacturerById(Long.parseLong(manuNum));
                    manuName = manufacturer.getManuName();

                }
                gt.setPosType(posType);
                gt.setSn(sn);
                gt.setManuNum(manuNum);
                gt.setManuName(manuName);
                gbTradeInfoTempMapper.deleteTrade(gt.getRrno());
                gbTradeInfoTempMapper.insertGbTradeInfoTemp(gt);
                gbTradeInfoTempMapper.deleteTradeError(data.getLocaldate().substring(0,10),tradeType.toString(),gt.getRrno());
                gbTradeInfoTempMapper.updateTradeError(data.getLocaldate().substring(0,10),tradeType.toString(),gt.getRrno());
            }

        }

        //List<GbTradeInfoGbTemp> gbList = util.importExcel(file.getInputStream());

        return 0;
    }
}
